package com.zsh.demo.controller;

import com.zsh.demo.dao.UserDao;
import com.zsh.demo.pojo.QueryInfo;
import com.zsh.demo.pojo.User;
import com.zsh.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class UsreController {
    @Autowired
    private UserService userService;

//    @RequestMapping("/users")
//    public ResponseEntity getUser(QueryInfo queryInfo){
//        System.out.println(queryInfo);
//        Integer number=userService.getUserCounts("%"+ queryInfo.getQuery()+"%");
//        Map<String,Object> maps=new HashMap<>();
//        List<User> listuser=userService.getAllUser("%"+ queryInfo.getQuery()+"%", queryInfo.getPageNum(), queryInfo.getPageSize());
//        maps.put("number",number);
//        maps.put("listuser",listuser);
//        System.out.println(number);
//        return ResponseEntity.ok().body(listuser);
//    }
@RequestMapping("/users")
public ResponseEntity getUser(@RequestParam("query") String query,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
    //System.out.println(queryInfo);
    Integer number=userService.getUserCounts("%"+ query+"%");
    Map<String,Object> maps=new HashMap<>();
    List<User> listuser=userService.getAllUser("%"+query +"%",pageNum,pageSize);
    if(listuser!=null) {
        maps.put("number", number);
        maps.put("listuser", listuser);
    }
//    System.out.println(number);
    return ResponseEntity.ok().body(maps);
}
    @RequestMapping("/states")
    public String states(@RequestParam("id") Integer id,@RequestParam("state") Boolean state){
     Integer result=userService.updateState(id,state);
     String str=result>0? "success":"error";
     return str;
    }
    @RequestMapping("/addUser")
    public ResponseEntity addUsers(@RequestBody User user){
      user.setRole("普通用户");
      user.setState(false);
      Integer result=userService.addUser(user);

     String str=result>0?"success":"error";
     return ResponseEntity.ok().body(str);
    }
    @RequestMapping("/deleteUser")
    public ResponseEntity deleteUsers(@RequestParam("id") Integer id){
    Integer result= userService.deleteUser(id);
    String str=result>0?"success":"error";
    return ResponseEntity.ok().body(str);
    }
    @RequestMapping("/getUser")
    public ResponseEntity getUser( Integer id){
     User u=userService.getUser(id);
        System.out.println(id);
     return ResponseEntity.ok().body(u);
    }
    @RequestMapping("/updateUser")
    public ResponseEntity updateUser(@RequestBody User user){
    Integer re=userService.updateUser(user);
    String str=re>0?"success":"error";
    return ResponseEntity.ok().body(str);
    }
}
